package com.snec.oauth2.oauth.jwt;

import com.snec.oauth2.oauth.tokenservice.MyDefaultTokenServices;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.JdkSerializationStrategy;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author 猪猪
 * @since 2022/7/1
 * 添加jwt令牌一些操作
 */
@Configuration
public class MyAuthorizationServerConfigurationBean {

    // 令牌存储
//    @Bean
//    public TokenStore jwtTokenStore(JwtAccessTokenConverter jwtAccessTokenConverter) {
//        return new JwtTokenStore(jwtAccessTokenConverter);
//    }
    //将令牌存入redis中
    @Bean
    public TokenStore tokenStore(RedisConnectionFactory connectionFactory) {
        RedisTokenStore tokenStore = new RedisTokenStore(connectionFactory);
        tokenStore.setSerializationStrategy(new JdkSerializationStrategy());
        tokenStore.setPrefix("user:session:");// 设置前缀
        return tokenStore;

    }
    /***
     * 自定义的token令牌
     * @param connectionFactory
     * @return
     */
    @Bean
    @Primary
    public MyDefaultTokenServices defaultTokenServices(RedisConnectionFactory connectionFactory) {
        MyDefaultTokenServices resourceServerTokenServices = new MyDefaultTokenServices();
        resourceServerTokenServices.setTokenStore(tokenStore(connectionFactory));
        return resourceServerTokenServices;
    }




    // Jwt转换器
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("123456");
        return converter;
    }
}
